#include "stdheader.h"

#include <string.h>

/////////////////////////////////////////////////////////
// Local vars
/////////////////////////////////////////////////////////

const float kfFadeDuration = 0.3f;

/////////////////////////////////////////////////////////

CBaseScreen::CBaseScreen() 
{
	m_eFadeState = EFadeState_None;
	m_tFadeRect.setSize(v2f(SC_WIDTH, SC_HEIGHT));

	m_tMsgBoxTex.loadFromFile("Resources/Art/MessageBox.png");

	for (int i = 0; i < MAX_TEXT_BOXES; i++)
	{
		m_pMsgBoxes[i] = nullptr;
		m_pText[i] = nullptr;
	}
}

CBaseScreen::~CBaseScreen()
{
}

/////////////////////////////////////////////////////////

void CBaseScreen::Process(float fTimeDelta)
{
	/////////////////////////////////////////////////////////
	// Message boxes

	for (int i = 0; i < MAX_TEXT_BOXES; i++)
	{
		if (m_pMsgBoxes[i] && m_fMsgDuration[i] > 0.0f)
		{
			m_fMsgDuration[i] -= fTimeDelta;

			if (m_fMsgDuration[i] <= 0.0f)
			{
				delete m_pMsgBoxes[i];
				m_pMsgBoxes[i] = nullptr;

				delete m_pText[i];
				m_pText[i] = nullptr;

				m_fMsgDuration[i] = 0.0f;
			}
		}
	}


	/////////////////////////////////////////////////////////
	// Fade processing
	if (m_eFadeState != EFadeState_None)
	{
		m_fFadeTimer -= fTimeDelta;

		switch (m_eFadeState)
		{
		case EFadeState_In:
			{
				sf::Uint8 iAlpha = (sf::Uint8)(255 * (1 - ((kfFadeDuration - m_fFadeTimer) / kfFadeDuration)));
				m_tFadeRect.setFillColor( sf::Color(0, 0, 0, iAlpha) );
				break;
			}
		case EFadeState_Out:
			{		
				int iAlpha = (int)(255 * ((kfFadeDuration - m_fFadeTimer) / kfFadeDuration));

				if (iAlpha > 255) 
					iAlpha = 255;

				m_tFadeRect.setFillColor( sf::Color(0, 0, 0, iAlpha) );
				break;
			}
		}

		if (m_fFadeTimer <= 0.f)
		{
			m_eFadeState = EFadeState_None;
			m_fFadeTimer = 0.f;
		}
	}
}

/////////////////////////////////////////////////////////

void CBaseScreen::Render(sf::RenderWindow* pWin)
{
	pWin->draw(m_tBG);

}

void CBaseScreen::RenderFade(sf::RenderWindow* pWin)
{
	// Isn't the fade (clearly) but needs to be rendered above all character/screen sprites
	for (int i = 0; i < MAX_TEXT_BOXES; i++)
	{
		if (m_pMsgBoxes[i] != nullptr)
		{
			pWin->draw(*m_pMsgBoxes[i]);
			pWin->draw(*m_pText[i]);
		}
	}

	if (m_eFadeState != EFadeState_None)
		pWin->draw(m_tFadeRect);
}

/////////////////////////////////////////////////////////

void CBaseScreen::ProcessInput(sf::Event& tEvent)
{

}

/////////////////////////////////////////////////////////

void CBaseScreen::StartFadeIn()
{
	m_fFadeTimer = kfFadeDuration;
	m_eFadeState = EFadeState_In;
}

/////////////////////////////////////////////////////////

void CBaseScreen::StartFadeOut()
{
	m_fFadeTimer = kfFadeDuration;
	m_eFadeState = EFadeState_Out;
}

/////////////////////////////////////////////////////////

void CBaseScreen::LayoutScreen()
{
}

/////////////////////////////////////////////////////////

void CBaseScreen::AddTextBox(const char* pStr, v2f vPosition, float fLifetime)
{
	for (int i = 0; i < MAX_TEXT_BOXES; i++)
	{
		if (m_pMsgBoxes[i] == nullptr)
		{
			m_pMsgBoxes[i] = new sf::Sprite(m_tMsgBoxTex);
			m_pMsgBoxes[i]->setPosition(vPosition.x - m_pMsgBoxes[i]->getGlobalBounds().width/2, vPosition.y - m_pMsgBoxes[i]->getGlobalBounds().height/2);
			m_fMsgDuration[i] = fLifetime;

			m_pText[i] = new sf::Text(pStr, CGame::Instance()->GetFont());
			v2f vCentre = v2f(m_pMsgBoxes[i]->getPosition().x + m_pMsgBoxes[i]->getGlobalBounds().width/2 - m_pText[i]->getGlobalBounds().width/2,
							  m_pMsgBoxes[i]->getPosition().y + m_pMsgBoxes[i]->getGlobalBounds().height/2 - m_pText[i]->getGlobalBounds().height/2);
			m_pText[i]->setPosition(vCentre);

			break;
		}
	}
}

/////////////////////////////////////////////////////////